package com.zyd.blog.homeart.service.impl;

import com.zyd.blog.homeart.service.ICommentService;
import com.zyd.blog.homeart.service.IDesignArticleService;
import com.zyd.blog.homeart.util.TimeUtil;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.mapper.*;
import me.zhyd.oauth.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.selector.Json;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liusongqi
 * @since 2020-02-11
 */
@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentReplyMapper commentReplyMapper;
    @Autowired
    private CommentRelationMapper commentRelationMapper;
    @Autowired
    private MyParticipateMapper myParticipateMapper;
    @Autowired
    private CenterMapper centerMapper;
    @Autowired
    private DesignArticleMapper designArticleMapper;
    @Autowired
    private SoftProductMapper softProductMapper;

    @Autowired
    private FineArtMapper fineArtMapper;
    @Autowired
    private TimeUtil timeUtil;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Integer addComment(Comment comment) throws Exception {
        int insert;
        comment.setDeleteState("1");
        comment.setCommentId(UuidUtils.getUUID());
        comment.setAddTime(sdf.format(System.currentTimeMillis()));
        comment.setUpdateTime(sdf.format(System.currentTimeMillis()));
        comment.setDiscussNumber(0);
        comment.setZanNumber(0);
        comment.setUseState("0");
        switch (comment.getUserId()) {
            case "1":
                comment.setUserName("用户名");
                comment.setUserImage("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2932779449,831692746&fm=26&gp=0.jpg");
                break;
            case "2":
                comment.setUserName("亚索");
                comment.setUserImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1652034236,78076834&fm=26&gp=0.jpg");
                break;
            case "3":
                comment.setUserName("卡牌大师");
                comment.setUserImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3940392874,3968870004&fm=26&gp=0.jpg");
                break;
            case "4":
                comment.setUserName("剑圣");
                comment.setUserImage("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=237035160,3996089600&fm=26&gp=0.jpg");
                break;
            case "2122338900":
                comment.setUserName("李青");
                comment.setUserImage("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1051951030,907960917&fm=26&gp=0.jpg");
                break;
        }
        try {
            insert = commentMapper.insert(comment);
        } catch (Exception e) {
            throw new Exception("添加评论失败");
        }
        if (comment.getState().equals("1")){
            try {
                designArticleMapper.commentNum(comment.getAnswerId());
            } catch (Exception e) {
                throw new Exception("jz评论数量+1失败");
            }
        }else if (comment.getState().equals("2")){
            try {
                softProductMapper.commentNum(comment.getAnswerId());
            } catch (Exception e) {
                throw new Exception("rz评论数量+1失败");
            }
        }else if (comment.getState().equals("3")){
            try {
                fineArtMapper.commentNum(comment.getAnswerId());
            } catch (Exception e) {
                throw new Exception("jp评论数量+1失败");
            }
        }

        MyParticipate myParticipate = new MyParticipate();
        myParticipate.setAddTime(sdf.format(System.currentTimeMillis()));
        myParticipate.setUserId(comment.getUserId());
        myParticipate.setState("2");
        myParticipate.setArticleId(comment.getAnswerId());
        myParticipate.setCategory(comment.getState());
        myParticipate.setParticipateId(UuidUtils.getUUID());
        Integer integer1 = myParticipateMapper.articleCount(comment.getAnswerId(), comment.getState());
        if (integer1==0){
            try {
                 myParticipateMapper.insert(myParticipate);
            } catch (Exception e) {
                throw new Exception("添加到我的参与失败");
            }
        }
        //查询该用户的 该篇文章 是否为第一次 点赞 评论 收藏
        Integer integer = centerMapper.centerCount(comment.getAnswerId(), comment.getUserId());
        if (integer == 0) {
            try {
                insert(comment.getUserId(), comment.getAnswerId());
            } catch (Exception e) {
                throw new Exception("添加center失败");
            }
        } else if (integer == 1) {
            try {
                update(comment.getUserId(), comment.getAnswerId());
            } catch (Exception e) {
                throw new Exception("修改center失败");
            }
        }
        return insert ;
    }

    @Override
    public List<CommentNew> threeComment(String answerId, String state, String userId) throws Exception {

        List<CommentNew> comments;
        try {
            comments = commentMapper.threeComment(answerId, state, userId);
        } catch (Exception e) {
            throw new Exception("获取最新评论失败");
        }
        for (CommentNew comment : comments) {
            String addTime = comment.getAddTime();
            int day = timeUtil.day(addTime);
            if (day == 0) {
                int minute = timeUtil.minute(addTime);
                if (minute == 0) {
                    comment.setAddTime("刚刚");
                } else if (minute > 0 & minute < 60) {
                    comment.setAddTime(minute + "分钟以前");
                } else if (minute >= 60 & minute < 1440) {
                    comment.setAddTime(minute / 60 + "小时以前");
                }
            } else if (day == -1) {
                comment.setAddTime("昨天" + comment.getAddTime().substring(11, 16));
            } else if (day == -2) {
                comment.setAddTime("前天" + comment.getAddTime().substring(11, 16));
            } else {
                comment.setAddTime(comment.getAddTime().substring(0, 16));
            }
        }
        return comments;
    }

    @Override
    public Map<String, List> Comment(String answerId, String state, String userId) throws Exception {
        SimpleDateFormat nomiao = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<CommentNew> comments;
        try {
            comments = commentMapper.comment(answerId, state, userId);
        } catch (Exception e) {
            throw new Exception("获取评论失败");
        }
        Map<String, List> map = new HashMap<>();
        List<Map> list = new ArrayList<>();

        int i;
        for (CommentNew comment : comments) {
            String addTime = comment.getAddTime();
            int day = timeUtil.day(addTime);
            if (day == 0) {
                int minute = timeUtil.minute(addTime);
                if (minute == 0) {
                    comment.setAddTime("刚刚");
                } else if (minute > 0 & minute < 60) {
                    comment.setAddTime(minute + "分钟以前");
                } else if (minute >= 60 & minute < 1440) {
                    comment.setAddTime(minute / 60 + "小时以前");
                }
            } else if (day == -1) {
                comment.setAddTime("昨天" + comment.getAddTime().substring(11, 16));
            } else if (day == -2) {
                comment.setAddTime("前天" + comment.getAddTime().substring(11, 16));
            } else {
                comment.setAddTime(comment.getAddTime().substring(0, 16));
            }
            List<CommentReplyNew> commentReplies;
            try {
                commentReplies = commentReplyMapper.commentReply(comment.getCommentId(), state, userId);
            } catch (Exception e) {
                throw new Exception("获取评论回复3条失败");
            }
            Map<String, Object> TowMap = new HashMap<>();
            map.put("comment", comments);
            if (commentReplies.size() == 0) {
                TowMap.put("list", new ArrayList<>());
                TowMap.put("count", 0);
            } else {
                CommentReply commentReply = new CommentReply();
                commentReply.setCommentId(comment.getCommentId());
                commentReply.setUseState("0");
                i = commentReplyMapper.selectCount(commentReply);
                TowMap.put("list", commentReplies);
                TowMap.put("count", i);
            }
            list.add(TowMap);
            System.out.println(list);
            map.put("commentReplies", list);
        }
        return map;
    }

    @Override
    public Integer fabulous(String commentId, String userId, String state) throws Exception {
        int fabulous;
        try {
            fabulous = commentMapper.fabulous(commentId);
        } catch (Exception e) {
            throw new Exception("点赞失败");
        }
        CommentRelation commentRelation = new CommentRelation();
        Integer integer = commentRelationMapper.commentCount(commentId, userId);
        if (integer == 0) {
            try {
                commentRelation.setId(commentId);
                commentRelation.setZan("0");
                commentRelation.setDiscuss("1");
                commentRelation.setUserId(userId);
                commentRelationMapper.insert(commentRelation);
            } catch (Exception e) {
                throw new Exception("添加commentRelation失败");
            }
        } else if (integer == 1) {
            try {
                commentRelationMapper.update(commentId, userId);
            } catch (Exception e) {
                throw new Exception("修改commentRelation失败");
            }
        }

        return fabulous;
    }

    @Override
    public Integer fabulousReply(String replyId, String userId) throws Exception {
        int fabulous;
        try {
            fabulous = commentReplyMapper.fabulous(replyId);
        } catch (Exception e) {
            throw new Exception("点赞失败");
        }
        CommentRelation commentRelation = new CommentRelation();
        Integer integer = commentRelationMapper.commentCount(replyId, userId);
        if (integer == 0) {
            try {
                commentRelation.setId(replyId);
                commentRelation.setZan("0");
                commentRelation.setDiscuss("1");
                commentRelation.setUserId(userId);
                commentRelationMapper.insert(commentRelation);
            } catch (Exception e) {
                throw new Exception("添加commentRelation失败");
            }
        } else if (integer == 1) {
            try {
                commentRelationMapper.update(replyId, userId);
            } catch (Exception e) {
                throw new Exception("修改commentRelation失败");
            }
        }

        return fabulous;
    }

    @Override
    public Integer reduceCommentZan(String commentId, String userId, String state) throws Exception {
        int fabulous;
        try {
            fabulous = commentMapper.reduceZan(commentId);
        } catch (Exception e) {
            throw new Exception("取消评论点赞失败");
        }
        try {
            commentRelationMapper.reduceZanUpdate(commentId, userId);
        } catch (Exception e) {
            throw new Exception("修改commentRelation失败");
        }

        return fabulous;
    }

    @Override
    public Integer reduceCommentReplyZan(String replyId, String userId) throws Exception {
        int fabulous;
        try {
            fabulous = commentReplyMapper.reduceZan(replyId);
        } catch (Exception e) {
            throw new Exception("取消评论回复点赞失败");
        }
        try {
            commentRelationMapper.reduceZanUpdate(replyId, userId);
        } catch (Exception e) {
            throw new Exception("修改commentRelation失败");
        }
        return fabulous;
    }

    @Override
    public Integer deleteComment(String designId,String commentId,String userId,String state) throws Exception {

        try {
            commentMapper.deleteComment(commentId);
        } catch (Exception e) {
            throw new Exception("删除评论失败");
        }
        try {
            commentRelationMapper.deleteCommentRelation(commentId);
        } catch (Exception e) {
            throw new Exception("删除comment_relation失败");
        }
        try {
            commentMapper.deleteCommentReply(commentId);
        } catch (Exception e) {
            throw new Exception("删除评论回复失败");
        }
        if (state.equals("1")){
            try {
                designArticleMapper.commentReduce(designId);
            } catch (Exception e) {
                throw new Exception("jz评论数量-1失败！");
            }
        }else if (state.equals("2")){
            try {
                softProductMapper.commentReduce(designId);
            } catch (Exception e) {
                throw new Exception("rz评论数量-1失败！");
            }
        }else if (state.equals("3")){
            try {
                fineArtMapper.commentReduce(designId);
            } catch (Exception e) {
                throw new Exception("jp评论数量-1失败！");
            }
        }

        try {
            if (commentMapper.num(designId,userId)==0){
                centerMapper.cancel(userId,designId,"2");
            }
        } catch (Exception e) {
            throw new Exception("修改center失败！");
        }
        try {
            myParticipateMapper.deleteComment(designId,userId);
        } catch (Exception e) {
            throw new Exception("从我的参与中删除失败");
        }
        return null;
    }

    @Override
    public Integer deleteShare(String id) throws Exception {
        String[] split = id.split(",");
        int integer = 0;
        try {
            for (String s : split) {
                integer = myParticipateMapper.deleteFenxaing(s);
            }
        } catch (Exception e) {
            throw new Exception("删除失败");
        }
        return integer;
    }

    @Override
    public Map<String, List> HTComment(String answerId,String state) throws Exception {
        SimpleDateFormat nomiao = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<Comment> comments;
        try {
            comments = commentMapper.HTcomment(answerId,state);
        } catch (Exception e) {
            throw new Exception("获取评论失败");
        }
        Map<String, List> map = new HashMap<>();
        List<Map> list = new ArrayList<>();

        int i;
        for (Comment comment : comments) {
            String addTime = comment.getAddTime();
            int day = timeUtil.day(addTime);
            if (day == 0) {
                int minute = timeUtil.minute(addTime);
                if (minute == 0) {
                    comment.setAddTime("刚刚");
                } else if (minute > 0 & minute < 60) {
                    comment.setAddTime(minute + "分钟以前");
                } else if (minute >= 60 & minute < 1440) {
                    comment.setAddTime(minute / 60 + "小时以前");
                }
            } else if (day == -1) {
                comment.setAddTime("昨天" + comment.getAddTime().substring(11, 16));
            } else if (day == -2) {
                comment.setAddTime("前天" + comment.getAddTime().substring(11, 16));
            } else {
                comment.setAddTime(comment.getAddTime().substring(0, 16));
            }
            List<CommentReplyNew> commentReplies;
            try {
                commentReplies = commentReplyMapper.HTcommentReply(comment.getCommentId());
            } catch (Exception e) {
                throw new Exception("获取评论回复3条失败");
            }
            Map<String, Object> TowMap = new HashMap<>();
            map.put("comment", comments);
            if (commentReplies.size() == 0) {
                TowMap.put("list", new ArrayList<>());
                TowMap.put("count", 0);
            } else {
                CommentReply commentReply = new CommentReply();
                commentReply.setCommentId(comment.getCommentId());
                i = commentReplyMapper.selectCount(commentReply);
                TowMap.put("list", commentReplies);
                TowMap.put("count", i);
            }
            list.add(TowMap);
            System.out.println(list);
            map.put("commentReplies", list);
        }
        return map;
    }

    //0收藏 1点赞 2评论 3分享
    private void insert(String userId, String id) {
        Center center = new Center();
        center.setArticleId(id);
        center.setUserId(userId);
        center.setDiscuss("0");
        center.setZan("1");
        center.setCollection("1");
        centerMapper.insert(center);
    }

    //0收藏 1点赞 2评论 3分享
    private void update(String userId, String id) {
        centerMapper.update(userId, id, "2");
    }
}
